package com.example.asus.a17376037_zq_market;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import util.HttpUtil;

public class SettingActivity extends AppCompatActivity  implements View.OnClickListener {
    EditText et_id1,et_telephone1,et_address1,password,password_confirm;
    Button btn_submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        init();
    }
    private void init() {
        et_id1 = findViewById(R.id.et_id1);
        et_telephone1 = findViewById(R.id.et_telephone1);
        et_address1 = findViewById(R.id.et_address1);
        password = findViewById(R.id.password);
        password_confirm = findViewById(R.id.password_confirm);
        btn_submit = findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("RegisteredActivity","点击");
        Toast.makeText(this,
                "修改中....", Toast.LENGTH_SHORT)
                .show();
        LonginActivity la=new LonginActivity();
        int aa=la.getUserId();
        String telephone=et_telephone1.getText().toString();
        String password1=password.getText().toString();
        String address=et_address1.getText().toString();
        String confirm=password_confirm.getText().toString();
        if(password1.equals(confirm)){

            String path ="http://106.13.133.171:80/MarketServer/UpdateUser?id="
                    +aa+"&password1="+password1+"&telephone="+telephone+"&address="+address;
            HttpUtil.sendHttpRequest(path,new Callback() {

                @Override
                public void onResponse(Call arg0, Response response) throws IOException {
                    Toast.makeText(getApplicationContext(),
                            "修改完成....", Toast.LENGTH_SHORT)
                            .show();
                    Log.d("RegisteredActivity","修改完成....");
                    // TODO Auto-generated method stubs
                    String responseData = response.body().string();
                    if(responseData.equals("修改成功")){
                        Intent in=new Intent(SettingActivity.this,MainActivity.class);
                        startActivity(in);
                        Log.d("RegisteredActivity",responseData);
                    }
                }

                @Override
                public void onFailure(Call arg0, IOException arg1) {
                    // TODO Auto-generated method stub
                    Log.d("RegisteredActivity","修改失败...");
                }
            });

        }
        else{

            Toast.makeText(getApplicationContext(),"两次密码输入不一致", Toast.LENGTH_SHORT).show();
        }
    }
    }
